<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import DataTable from '../components/common/DataTable.vue'


// 列配置
const columns = [
  { prop: 'id', label: '业主ID', width: '80', sortable: true },
  { prop: 'owner', label: '业主' },
  { prop: 'gender', label: '性别' },  
  { prop: 'age', label: '年龄' },    
  { prop: 'tel', label: '联系方式' },  
  { prop: 'dueDate', label: '入住时间', type: 'date', sortable: true },
  
]

// 模拟数据
const tableData = ref([])
const loading = ref(false)

// 生成模拟数据
const generateMockData = () => {
  const data = []
  const statusOptions = ['详细']  
  const owners = ['陆林轩', '袁天罡','蚩梦', '唐三', '朱竹清', '叶修','叶秋', '李星云', '降臣','姬如雪', '张子凡', '宁荣荣','戴沐白', '千仞雪']    
  const buildings = ['A栋', 'B栋', 'C栋']
  const genders = ['男', '女']  
  const ages = ['18', '20', '21', '40', '41','43', '28', '36']    
  
  for (let i = 1; i <= 100; i++) {
    const building = buildings[Math.floor(Math.random() * buildings.length)]
    const unit = Math.floor(Math.random() * 10) + 1
    const room = Math.floor(Math.random() * 5) + 1
    const houseNumber = `${building}${unit}0${room}`
    
    data.push({
      id: i+1000000,
      owner: owners[Math.floor(Math.random()*owners.length)], 
      gender: genders[Math.floor(Math.random()*genders.length)],
      age: ages[Math.floor(Math.random()*ages.length)],           
      tel: Math.floor(Math.random() * 5000) + 18788880000,      
      dueDate: new Date(2024, Math.floor(Math.random() * 12), Math.floor(Math.random() * 28) + 1).toISOString(),
      comments: `备注信息${i}`
    })
  }
  
  return data
}

// 事件处理器
const handleRowAdd = (row) => {
  // 在真实环境中，这里应该是API调用
  console.log('添加行:', row)
  // 生成ID并添加到表格
  const newRow = {
    ...row,
    id: tableData.value.length + 1,
    dueDate: row.dueDate || new Date().toISOString(),
    status: row.status || 'unpaid'
  }
  tableData.value.push(newRow)
}

const handleRowEdit = (row) => {
  // 在真实环境中，这里应该是API调用
  console.log('编辑行:', row)
  const index = tableData.value.findIndex(item => item.id === row.id)
  if (index !== -1) {
    tableData.value[index] = row
  }
}


const handleRowDelete = (row) => {
  // 在真实环境中，这里应该是API调用
  console.log('删除行:', row)
  tableData.value = tableData.value.filter(item => item.id !== row.id)
}

// 模拟加载数据
const loadData = () => {
  loading.value = true
  // 模拟网络请求延迟
  setTimeout(() => {
    tableData.value = generateMockData()
    loading.value = false
  }, 500)
}

onMounted(() => {
  loadData()
})
</script>

<template>
  <div class="property-fee-container">
    <div class="page-header bg-white p-4 rounded-lg shadow-sm mb-4">
      <h1 class="text-xl font-bold text-gray-800">业主管理</h1>
      <p class="text-gray-500 mt-1">查看和管理所有住户房屋情况</p>
    </div>
    <div class="data-table-wrapper bg-white p-4 rounded-lg shadow-sm">
      <DataTable
        :data="tableData"
        :columns="columns"
        :loading="loading"
        @row-all="showData"
        @row-add="handleRowAdd"        
        @row-edit="handleRowEdit"
        @row-delete="handleRowDelete"
      />
    </div>
  </div>
</template>

<style scoped>
.property-fee-container {
  min-height: 100%;
}

.data-table-wrapper {
  overflow: auto;
}
</style> 